<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>评论列表</title>
  <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
  <link rel="stylesheet" href="./css/reset.css">
  <link rel="stylesheet" href="./css/iconfont.css">
  <link rel="stylesheet" href="./css/main.css">
  <link rel="stylesheet" href="./libs/layui/css/layui.css">
  <link rel="stylesheet" href="./libs/layui/css/modules/layer/default/layer.css">
  <script src="./libs/jquery/jquery.min.js"></script>
  <script src="../js/template-web.js"></script>
  <script src="./libs/layui/layui.js"></script>
  <script src="./libs/layui/lay/modules/layer.js"></script>



</head>

<body>

  <div class="container-fluid">
    <div class="common_title">
      评论列表
    </div>
    <div class="container-fluid common_con">
      <table class="table table-striped table-bordered table-hover mp20">
        <thead>
          <tr>
            <th>作者</th>
            <th>评论</th>
            <th>评论在</th>
            <th>提交于</th>
            <th>状态</th>
            <th class="text-center" width="150">操作</th>
          </tr>
        </thead>
        <tbody id="tbody">
          <tr>
            <td>小周</td>
            <td>这是一条测试评论，欢迎光临</td>
            <td>《世界，你好》</td>
            <td>2017-07-04 12:00:00</td>
            <td>已批准</td>
            <td class="text-center">
              <a href="javascript:void(0);;" class="btn btn-warning btn-xs">拒绝</a>
              <a href="javascript:void(0);;" class="btn btn-danger btn-xs">删除</a>
            </td>
          </tr>
          <tr class="danger">
            <td>中周</td>
            <td>How are you?</td>
            <td>《世界，你好》</td>
            <td>2017-07-09 18:22:00</td>
            <td>待审核</td>
            <td class="text-center">
              <a href="javascript:void(0);;" class="btn btn-info btn-xs">批准</a>
              <a href="javascript:void(0);;" class="btn btn-danger btn-xs">删除</a>
            </td>
          </tr>

          <tr>
            <td>武秀英</td>
            <td>外影广条同取水权科速工与。矿身面却属次养导务作者用品油调。高石期品极放存斗一号口消别共去。</td>
            <td>《世界，你好》</td>
            <td>1970-03-15 11:31:50</td>
            <td>已拒绝</td>
            <td class="text-center">
              <a href="javascript:void(0);;" class="btn btn-danger btn-xs">删除</a>
            </td>
          </tr>

        </tbody>
      </table>
      <div class="row text-center" id="test1">

        <ul id="pagination" class="pagination-sm"></ul>
      </div>

    </div>
  </div>
  <script src="./js/http.js"></script>
  <script id="art_list" type="text/html">
    {{each data}}
    {{if $value.state =='已拒绝'}}
    <tr class="danger">
      {{else}}
    <tr>
    {{/if}}
    <td>{{$value.author}}</td>
    <td>{{$value.content}}</td>
    <td>{{$value.title}}</td>
    <td>{{$value.date}}</td>
    <td>{{$value.state}}</td>
    <td class='text-center'>
    {{if $value.state=='待审核'}}
      <a href="javascript:void(0);" onclick="accept({{$value.id}})" class="btn btn-success btn-xs btnAccept">批准</a>
      <a href="javascript:void(0);" onclick="reject({{$value.id}})" class="btn btn-warning btn-xs btnReject">拒绝</a>
    {{else if $value.state=='已通过'}}
    <a href="javascript:void(0);" onclick="reject({{$value.id}})" class="btn btn-warning btn-xs btnReject">拒绝</a>
    {{else}}
    <a href="javascript:void(0);" onclick="accept({{$value.id}})" class="btn btn-success btn-xs btnAccept">批准</a>
    {{/if}}
      <a href="javascript:void(0);" onclick="del({{$value.id}})" class="btn btn-danger btn-xs btnReject">删除</a>
    </td>
  </tr>
    {{/each}}
  </script>
  <script>

    $(function () {
      get_list();



    })


    //获取列表数据
    function get_list(curr, limit) {
      let pg_params = {
        page: curr,
        perpage: limit

      }

      myAjax('get', 'http://localhost:8080/api/v1/admin/comment/search', pg_params, function (resData) {
        console.log(resData);

        let resHtml = template('art_list', resData.data)

        $('#tbody').html(resHtml)

        render_pager(resData.data.totalCount, curr)
      });
    };


    //生成分页控件
    function render_pager(totalCount, curr) {
      layui.use('laypage', function () {
        //获取到分页控件对象
        var laypage = layui.laypage;
        //执行一个laypage实例
        laypage.render({
          elem: 'test1' //注意，这里的 test1 是 ID，不用加 # 号
          , count: totalCount, //数据总数，从服务端得到
          limit: 6,   //每页显示条数
          curr: curr, //当前页码
          jump: function (obj, first) {
            // alert(first)
            //obj包含了当前分页的所有参数.ex:(cyrr:得到当前页,limit:得到每页显示的条数)
            if (!first) {
              get_list(obj.curr, obj.limit);
            }

          }
        });
      })

    }


    //拒绝按钮
    function reject(c_id) {

      layer.confirm('您真的要拒绝这条评论?', { icon: 3, title: '提示' }, function (index) {
        //do something
        myAjax('post', 'http://localhost:8080/api/v1/admin/comment/reject', { id: c_id }, function (resData) {
          if (resData.code !== 200) {
            layer.msg(resData.msg)
            return;
          }
          get_list()
        })
        layer.close(index);
      });
    }

    //批准按钮
    function accept(c_id) {

      layer.confirm('您真的要批准这条评论?', { icon: 3, title: '提示' }, function (index) {
        //do something
        myAjax('post', 'http://localhost:8080/api/v1/admin/comment/pass', { id: c_id }, function (resData) {
          if (resData.code !== 200) {
            layer.msg(resData.msg)
            return;
          }
          get_list()
        })
        layer.close(index);
      });

    }


    //删除按钮
    function del(c_id) {

      layer.confirm('您真的要delete这条评论?', { icon: 3, title: '提示' }, function (index) {
        //do something
        myAjax('post', 'http://localhost:8080/api/v1/admin/comment/delete', { id: c_id }, function (resData) {
          if (resData.code !== 200) {
            layer.msg(resData.msg)
            return;
          }
          get_list()
        })
        layer.close(index);
      });

    }

  </script>
</body>

</html>